#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 1010, INF = 1e9;
int n, m, k;
int w[N][N], minv[N][N], maxv[N][N];
int q[N], a[N], b[N], c[N], d[N];

void get_max(int a[], int b[], int m) {
	int h = 1, t = 0;
	for (int i = 1; i <= m; i++) {
		while (h <= t && a[q[t]] <= a[i]) t--;
		q[++t] = i;
		if (q[h] < i - k + 1) h++;
		b[i] = a[q[h]];
	}
}

void get_min(int a[], int b[], int m) {
	int h = 1, t = 0;
	for (int i = 1; i <= m; i++) {
		while (h <= t && a[q[t]] >= a[i]) t--;
		q[++t] = i;
		if (q[h] < i - k + 1) h++;
		b[i] = a[q[h]];
	}
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> w[i][j];
		}
	}

	for (int i = 1; i <= n; i++) {
		get_max(w[i], maxv[i], m);
		get_min(w[i], minv[i], m);
	}
	int res = INF;
	for (int j = k; j <= m; j++) {
		for (int i = 1; i <= n; i++) {
			a[i] = maxv[i][j];
			b[i] = minv[i][j];
		}
		get_max(a, c, n);
		get_min(b, d, n);
		for (int i = k; i <= n; i++) res = min(res, c[i] - d[i]);
	}
	cout << res << endl;
	return 0;
}